// hal驱动库, 基于AT32F403A
// author zf
// date 2024/08/05
#ifndef HAL_DRV_H
#define HAL_DRV_H

#include "at32f403a_407_conf.h"
#include "stdio.h"

/* @} */
/*! @brief Construct a status code value from a group and code number. */
#define MAKE_STATUS(group, code) ((((group) * 100) + (code)))
/*! @brief Type used for all status and error return values. */
typedef int32_t hal_status_t;
/*! @brief Status group numbers. */
enum _status_groups
{
    kHalStatusGroup_Generic = 0, /*!< Group number for generic status codes. */
};
/*! \public
 * @brief Generic status return codes.
 */
enum
{
    kHalStatus_Success = MAKE_STATUS(kHalStatusGroup_Generic, 0),              /*!< Generic status for Success. */
    kHalStatus_Fail = MAKE_STATUS(kHalStatusGroup_Generic, 1),                 /*!< Generic status for Fail. */
    kHalStatus_ReadOnly = MAKE_STATUS(kHalStatusGroup_Generic, 2),             /*!< Generic status for read only failure. */
    kHalStatus_OutOfRange = MAKE_STATUS(kHalStatusGroup_Generic, 3),           /*!< Generic status for out of range access. */
    kHalStatus_InvalidArgument = MAKE_STATUS(kHalStatusGroup_Generic, 4),      /*!< Generic status for invalid argument check. */
    kHalStatus_Timeout = MAKE_STATUS(kHalStatusGroup_Generic, 5),              /*!< Generic status for timeout. */
    kHalStatus_NoTransferInProgress = MAKE_STATUS(kHalStatusGroup_Generic, 6), /*!< Generic status for no transfer in progress. */
    kHalStatus_Waiting = MAKE_STATUS(kHalStatusGroup_Generic, 7),
};

/*****************************************************************************
 * gpio抽象api
 * 
 */
typedef gpio_type hal_hw_gpio_t;
#define hal_hw_pin_reset gpio_bits_reset
#define hal_hw_pin_set gpio_bits_set
#define hal_hw_pin_write gpio_bits_write
#define hal_hw_pin_read gpio_input_data_bit_read

enum {
    PA = 0,
    PB = 1,
    PC = 2,
    PD = 3,
    PE = 4,
    PF = 5,
    PG = 6,
    PH = 7,
    PI = 8,
};

typedef tmr_type hal_hw_pwm_t;
#define HAL_PWM_LEVEL_MAX       (10000)

enum {
    T1 = 0,
    T2 = 1,
    T3 = 2,
    T4 = 3,
    T5 = 4,
    T6 = 5,
    T7 = 6,
    T8 = 7,
    T9 = 8,
    T10 = 9,
    T11 = 10,
    T12 = 11,
    T13 = 12,
    T14 = 13,
};

typedef union
{
    struct
    {
        uint16_t ch     :8;
        uint16_t group  :8;
    };
    uint16_t port;
} hal_gpio_t;

typedef hal_gpio_t hal_pwm_t;

#define hal_port(g, c) (((uint16_t)g << 8) | c)

extern gpio_type *gpio_group[];
extern hal_hw_pwm_t *pwm_group[];
extern uint8_t pwm_ch[];
static inline void hal_pin_write(hal_gpio_t *gpio, uint8_t level)
{
    hal_hw_pin_write(gpio_group[gpio->group], 1ul << gpio->ch, level > 0);
};
static inline void hal_pin_set(hal_gpio_t *gpio)
{
    hal_hw_pin_set(gpio_group[gpio->group], 1ul << gpio->ch);
};
static inline void hal_pin_reset(hal_gpio_t *gpio)
{
    hal_hw_pin_reset(gpio_group[gpio->group], 1ul << gpio->ch);
};
static inline uint8_t hal_pin_read(hal_gpio_t *gpio)
{
    return hal_hw_pin_read(gpio_group[gpio->group], 1ul << gpio->ch);
};
static inline void hal_set_pin_out(hal_gpio_t *gpio)
{

};
static inline void hal_set_pin_in(hal_gpio_t *gpio)
{

};

typedef uint32_t    hal_spi_t;
typedef uint32_t    hal_i2c_t;
typedef uint32_t    hal_dma_t;

#define DELAY_US_TICK   (25)

extern void hal_delay_us(uint32_t us);
extern void hal_delay_ms(uint32_t ms);

#endif
